﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TTBViewer
{
	public partial class MainForm : Form
	{
		private Bitmap tileImage;

		private BufferedGraphicsContext context;
		private BufferedGraphics grafx;

		private string ttbFilePath = null;

		public MainForm()
		{
			context = BufferedGraphicsManager.Current;
		}

		public MainForm(string filePath)
		{
			context = BufferedGraphicsManager.Current;

			ttbFilePath = filePath;
		}

		private string OpenTTBFile()
		{
			OpenFileDialog fopenDlg = new OpenFileDialog();

			fopenDlg.Filter = "TTB Files|*.ttb";
			fopenDlg.Multiselect = false;

			if (fopenDlg.ShowDialog() == DialogResult.OK)
				return fopenDlg.FileName;
			else
				return null;
		}

		private void LoadTTBFile(string filePath)
		{
			FileStream fs = new FileStream(filePath, FileMode.Open);
			BinaryReader br = new BinaryReader(fs);

			int tileWidth = br.ReadInt32();
			int tileHeight = br.ReadInt32();

			tileImage = new Bitmap(tileWidth, tileHeight, PixelFormat.Format32bppRgb);

			for (int i = 0; i < tileWidth; i++)
			{
				for (int j = 0; j < tileHeight; j++)
				{
					if (br.ReadByte() == 0)
						tileImage.SetPixel(i, j, Color.White);

					br.ReadByte();
				}
			}

			tileImage.RotateFlip(RotateFlipType.Rotate90FlipXY);

			br.Close();
			fs.Close();

			this.ClientSize = new Size(tileWidth, tileHeight);
			this.label1.Visible = false;

			context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
			grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height));
		}

		private void DrawTTB()
		{
			grafx.Graphics.FillRectangle(Brushes.Black, 0, 0, context.MaximumBuffer.Width, context.MaximumBuffer.Width);

			grafx.Graphics.DrawImage(tileImage, 0, 0);

			grafx.Render();
		}

		private bool IsTTBFile(string filePath)
		{
			char[] delimiter = { '.' };
			string[] split = filePath.Split(delimiter);

			if (split[split.Length - 1].CompareTo("ttb") == 0 ||
			split[split.Length - 1].CompareTo("TTB") == 0)
				return true;
			else
				return false;
		}

		//--------
		private void MainForm_Paint(object sender, PaintEventArgs e)
		{
			if (grafx != null)
				grafx.Render();
		}

		private void MainForm_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
				e.Effect = DragDropEffects.All;
			else
				e.Effect = DragDropEffects.None;
		}

		private void MainForm_DragDrop(object sender, DragEventArgs e)
		{
			string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);

			if (!IsTTBFile(filePath[0]))
				return;

			LoadTTBFile(filePath[0]);

			DrawTTB();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			if (ttbFilePath != null)
			{
				if (!IsTTBFile(ttbFilePath))
					return;

				LoadTTBFile(ttbFilePath);

				DrawTTB();
			}
		}

		private void MainForm_MouseMove(object sender, MouseEventArgs e)
		{
			if (grafx != null)
			{
				int vertexX = e.X;
				int vertexY = (-1 * e.Y) + (this.ClientSize.Height - 1);
				int mapX = vertexX / 2;
				int mapY = vertexY / 2;

				string tooltipText = vertexX + " / " + vertexY + "\n" + mapX + " / " + mapY;

				toolTip1.Show(tooltipText, this, e.X + 16, e.Y + 32);
			}
		}

		private void MainForm_MouseWheel(object sender, MouseEventArgs e)
		{
			if ((e.Delta / 120) > 0)
			{
				// Up
				this.Opacity += 0.1;
			}
			else
			{
				// Down
				this.Opacity -= 0.1;
				if (this.Opacity < 0.2)
					this.Opacity = 0.2;
			}
		}
	}
}